package com.qf.filter;

import com.alibaba.fastjson.JSONObject;
import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import com.netflix.zuul.exception.ZuulException;
import com.qf.Resp.BaseResp;
import com.qf.utils.JWTUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.netflix.zuul.filters.support.FilterConstants;
import org.springframework.stereotype.Component;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author H
 * @create 2020-11-03 10:05
 * @strive To be whoever you want to be.
 * @Description:
 */
//@Component
public class AuthorZuulFiter extends ZuulFilter {

    private static List<String> url = new ArrayList<>();

    @Autowired
    JWTUtils jwtUtils;

    @Override
    public String filterType() {
        return FilterConstants.PRE_TYPE;
    }

    @Override
    public int filterOrder() {
        return FilterConstants.PRE_DECORATION_FILTER_ORDER;
    }

    @Override
    public boolean shouldFilter() {
        RequestContext currentContext = RequestContext.getCurrentContext();
        HttpServletRequest request = currentContext.getRequest();
        String requestURI = request.getRequestURI();
        if (url.contains(requestURI)) {
            return false;
        }
        return true;
    }

    @Override
    public Object run() throws ZuulException {
        RequestContext currentContext = RequestContext.getCurrentContext();
        HttpServletRequest request = currentContext.getRequest();
        Cookie[] cookies = request.getCookies();
        String token = null;
        for (Cookie cook : cookies
        ) {
            if (cook.getName().equals("token")) {
                token = cook.getValue();
            }
        }
        Map verify = jwtUtils.Verify(token);
        BaseResp baseResp = new BaseResp();
        if (verify == null) {
            currentContext.getResponse().setContentType("application/json;charset=utf-8");
            baseResp.setCode(10003l);
            baseResp.setMessage("用户token验证失败。");
            currentContext.setResponseBody(JSONObject.toJSON(baseResp).toString());
            //在这里截止不允许继续执行
            currentContext.setSendZuulResponse(false);
        }
        return null;
    }

    private AuthorZuulFiter() {
        url.add("/credit-user/login");
        url.add("/credit-user/registry2");
        url.add("/credit-user/sendCode");

      /*  url.add("/credit-personal/findPersonal");
        url.add("/credit-personal/findPartMessage");
        url.add("/credit-personal/findPartMessageByName");
        url.add("/credit-personal/findPersonal");//管理员的看所有用户的几表联查

        url.add("/credit-loanapplication/findAllAssetsappraisal");
        url.add("/credit-loanapplication/findAllByBorrowerId");
        url.add("/credit-loanapplication/findByBorrowerIdOrCertificateNameContaining");
        url.add("/credit-loanapplication/findByAssetsappraisalId");
        url.add("/credit-loanapplication/saveOrUpdateAssetsappraisal");
        url.add("/credit-loanapplication/deleteByAssetsappraisalId");
        url.add("/credit-loanapplication/upload");*/

    }
}
